package com.koron.css2.baseConfig.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MatrBean;
import com.koron.css2.baseConfig.bean.MatrCodeBean;
import com.koron.css2.baseConfig.mapper.MatrMapper;
import com.koron.css2.baseConfig.queryBean.MatrQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.MatrCodeMapper;
import com.koron.css2.serviceManage.queryBean.matrCodeQueryBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.mysql.cj.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 材料价格-列表初始化
 *
 * @author 廖大富
 */
public class MatrList implements ServerInterface {

    private static Logger logger = LogManager.getLogger(MatrList.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

        @SuppressWarnings("rawtypes")
        MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
        try {
            MatrQueryBean bean = JsonUtils.objectToPojo(req.getData(), MatrQueryBean.class);
            MatrMapper mapper = factory.getMapper(MatrMapper.class);
            MatrCodeMapper matrCodeMapper = factory.getMapper(MatrCodeMapper.class, "_default");
            matrCodeQueryBean selectBean = new matrCodeQueryBean();
            if (StringUtils.isNullOrEmpty(bean.getMatrNo())) {
                selectBean.setClassCode("");
            }else {
                MatrCodeBean matrCodeBean = matrCodeMapper.selectMatrTemplateByNo(bean.getMatrNo());
                selectBean.setClassCode(matrCodeBean.getClassCode());
            }
            selectBean.setIsLeaf(1);
         // 设置分页参数
         	PageHelper.startPage(bean.getPage(), bean.getPageCount());
            List<MatrCodeBean> matrCodeBeans = matrCodeMapper.selectMatrCodeList(selectBean);
            List<MatrBean> list = mapper.selectMatrPrice();
            for (MatrBean matrBean : list) {
                for (MatrCodeBean codeBean : matrCodeBeans) {
                    if (matrBean.getMatrNo().equals(codeBean.getMatrNo())) {
                        codeBean.setPrice(matrBean.getMatrPrice());
                        codeBean.setMatrPriceId(matrBean.getId());
                        break;
                    }
                }
            }
            info.setData(new PageInfo<>(matrCodeBeans));
        } catch (Exception e) {
            info.setCode(Constant.MESSAGE_INT_FAIL);
            info.setDescription(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return info;
    }
}
